export default {
    data() {
        return {
            visible: false,
            dataSource: [],
            loading: false,
            pageInfo: { page: 1, pageSize: 15, total: 0, filters: [], sorts: [] },
            filterModel: {},
            fixedFilters: [],
            entityInfo: {},
            rowKey: '',
            queryUrl: '',
            saveUrl: '',
            removeUrl: '',
            // 当前选择项目
            selectRows: [],
            // 当前选择的主键集合
            selectRowIds: [],
            // 展开行主键值
            expandRowKeys: [],
            // 表格ref引用，存在时单击和切换选择状态，多选时有效
            tableRef: '',
            // 选中数量
            selectCount: 9999
        }
    },
    computed: {
        loginUser(){
            return this.$store.state.userInfos.userInfo;
        }
    },
    watch: {
        visible(){
            if (!this.visible) {
                this.initEntity()
            }
        }
    },
    methods: {
        initEntity(){
            this.entityInfo = {}
        },
        addEntity() {
            this.visible = true
        },
        editEntity(row) {
            this.entityInfo = this.$Util.copy(row)
            this.visible = true
        },
        copyEntity(row) {
            this.entityInfo = this.$Util.copy(row)
            Reflect.set(this.entityInfo, 'copyId', this.entityInfo[this.rowKey])
            Reflect.deleteProperty(this.entityInfo, this.rowKey)
            this.visible = true
        },
        filterChange(prop, op){
            const value = this.filterModel[prop]
            if (this.$Util.isEmpty(value)) {
                const index = this.fixedFilters.findIndex(item => { return item.field === prop})
                if (index > -1) {
                    this.fixedFilters.splice(index, 1)
                }
            } else {
                this.addFilter({ field: prop, op: op || 'like', value}, this.fixedFilters)
            }
            this.loadSource()
        },
        addFilter(filter, filters){
            const list = filters || this.fixedFilters
            const index = list.findIndex(item => { return item.field === filter.field})
            if (index < 0) {
                list.push(filter)
            } else {
                list.splice(index, 1, filter)
            }
        },
        delFilter(field, filters){
            const list = filters || this.fixedFilters
            const index = list.findIndex(item => { return item.field === field})
            if (index > -1) {
                list.splice(index)
            }
        },
        handleSizeChange(val) {
            this.pageInfo.pageSize = val
            this.loadSource()
        },
        handleCurrentChange(val) {
            this.pageInfo.page = val
            this.loadSource()
        },
        handleRowClick(row, column){
            if (!column || !column.property || (row && row.editAbled)){
                return
            }
            const $table = this.$refs[this.tableRef]
            if ($table) {
                if (this.selectCount === 1 && this.selectRows.length > 0) {
                    const keyVal = row[this.rowKey]
                    const index = this.selectRows.findIndex(item => item[this.rowKey] === keyVal)
                    if (index < 0) {
                        $table.clearSelection()
                    }
                }
                $table.toggleRowSelection(row)
            }
            this.$emit('rowClick', row, column)
            this.rowClick(row)
        },
        rowClick(row){

        },
        selectAll(selection) {
            this.selectRows = this.$Util.copy(selection)
            this.emitSelect()
        },
        selectSingle(selection, row) {
            this.selectRows = this.$Util.copy(selection)
            this.emitSelect()
        },
        emitSelect() {
            this.selectRowIds = []
            if (this.rowKey) {
                this.selectRows.forEach(item => {
                    this.selectRowIds.push(item[this.rowKey])
                })
            }
            this.$emit('select', this.selectRows, this.selectRowIds)
        },
        loadSource(filters) {
            if (this.$Util.isEmpty(this.queryUrl)) {
                return
            }
            this.loading = true
            this.pageInfo.filters = this.$Util.copy(this.fixedFilters)
            if (!this.$Util.isEmpty(filters)) {
                filters.forEach(item => {
                    const index = this.pageInfo.filters.findIndex(e => { return e.field === item.field })
                    if (index < 0) {
                        this.pageInfo.filters.push(item)
                    } else {
                        this.pageInfo.filters.splice(index, 1, item)
                    }
                })
            }
            this.$post(this.appendUrl(this.queryUrl), this.pageInfo)
                .then(res => {
                    this.$resultMsg(this, res, true)
                    if (res.code === 200) {
                        const { total, records } = res.data
                        this.dataSource = records
                        this.pageInfo.total = total
                        this.loadComplate()
                    }
                    setTimeout(() => {
                        this.loading = false
                    },100)
                })
        },
        loadComplate() {

        },
        appendUrl(url) {
            return url
        },
        saveData(data, notClose, callBack) {
            if (!this.saveUrl) {
                return
            }
            this.loading = true
            this.$post(this.appendUrl(this.saveUrl), data)
                .then(res => {
                    this.$resultMsg(this, res)
                    if (res.code === 200) {
                        if (!notClose) {
                            this.visible = false
                        }
                        this.loadSource()
                    }
                    if (callBack instanceof Function){
                        callBack(res)
                    }
                    this.loading = false
                })
        },
        removeData(data, index) {
            const keyVal = data[this.rowKey]
            if (keyVal) {
                if (!this.removeUrl) {
                    this.dataSource.splice(index, 1)
                    return
                }
                this.$remove(this, this.appendUrl(this.removeUrl + keyVal))
                    .then(res => {
                        this.$resultMsg(this, res)
                        if (res.code === 200) {
                            this.loadSource()
                            this.$emit('rowRemove', data, keyVal)
                        }
                    })
            } else {
                this.dataSource.splice(index, 1)
            }
        }
    }
}
